package com.example.demo.controller;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.config.AppConfig;
import com.example.demo.model.Article;
import com.example.demo.model.ArticleReply;
import com.example.demo.model.User;
import com.example.demo.services.IArticleReplyService;
import com.example.demo.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author baekhyun
 **/
@Api(tags = "回复接口")
@RestController
@RequestMapping("/reply")
@Slf4j
public class ArticleReplyController {

    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IArticleService articleService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("帖子正文") @RequestParam("content") @NonNull String content){
        //1、校验用户状态是否为禁言
        HttpSession session= request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION_KEY);
        if (user.getState()==1){
            //返回错误描述
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //2、校验帖子是否存在
        Article article=articleService.selectDetailById(articleId);
        if (article==null || article.getDeleteState()==1){
            //返回错误描述
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //3、校验帖子状态是否正常
        if (article.getState()==1){
            //返回错误描述
            return AppResult.failed(ResultCode.FAILED_ARTICLE_STATE);
        }
        //4、构造回复对象
        ArticleReply articleReply=new ArticleReply();
        articleReply.setPostUserId(user.getId());//回复的作者
        articleReply.setArticleId(articleId);//主贴Id
        articleReply.setContent(content);//回复内容
        //5、调用service
        articleReplyService.create(articleReply);
        //6、返回正确结果
        return AppResult.success("回复成功");
    }

    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("主帖Id") @RequestParam("articleId") @NonNull Long articleId){
        //校验帖子是否存在
        Article article=articleService.selectDetailById(articleId);
        if (article==null || article.getDeleteState()==1){
            //返回错误描述
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //调用service获取回复列表
        List<ArticleReply> articleReplies=articleReplyService.selectByArticleId(articleId);
        //校验结果是否为空，为空的话创建一个空集合并返回([])，返回一个字符串形式的null
        if (articleReplies==null){
            articleReplies=new ArrayList<>();
        }
        //返回结果
        return AppResult.success(articleReplies);
    }
}
